﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YJKRevitFamilyDataBase;

namespace YJKFamilyLibManager.FamilyLibModule
{
	public partial class DlgEditFamily : System.Windows.Forms.Form
	{
		public DlgEditFamily(IDlgBase main)
		{
			this.mForm = main;
			this.InitializeComponent();
		}

		private void btnToRight_Click(object sender, EventArgs e)
		{
			this.toRight();
		}

		private void btnToLeft_Click(object sender, EventArgs e)
		{
			this.toLeft();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
		}

		private void tbxParamName_KeyPress(object sender, KeyPressEventArgs e)
		{
		}

		private void lbxParam_DoubleClick(object sender, EventArgs e)
		{
			this.toRight();
		}

		private void toRight()
		{
			if (this.lbxParam.SelectedItem != null)
			{
				int selectedIndex = this.lbxParam.SelectedIndex;
				DbParamDefinition item = (DbParamDefinition)this.lbxParam.SelectedItem;
				this.lbxParam.Items.Remove(this.lbxParam.SelectedItem);
				this.lbxRecParam.Items.Add(item);
				if (selectedIndex < this.lbxParam.Items.Count)
				{
					this.lbxParam.SelectedIndex = selectedIndex;
					return;
				}
				this.lbxParam.SelectedIndex = selectedIndex - 1;
			}
		}

		private void toLeft()
		{
			if (this.lbxRecParam.SelectedItem != null)
			{
				int selectedIndex = this.lbxParam.SelectedIndex;
				DbParamDefinition dbParamDefinition = (DbParamDefinition)this.lbxRecParam.SelectedItem;
				if (dbParamDefinition.Limit)
				{
					MessageBox.Show("不能移除关键属性", "族库管理");
					return;
				}
				if (dbParamDefinition.ParamDefinitionType == ParamDefinitionType.InstanceParam)
				{
					MessageBox.Show("不能移除实例属性", "族库管理");
					return;
				}
				this.lbxRecParam.Items.Remove(dbParamDefinition);
				this.lbxParam.Items.Add(dbParamDefinition);
				if (selectedIndex < this.lbxParam.Items.Count)
				{
					this.lbxRecParam.SelectedIndex = selectedIndex;
					return;
				}
				this.lbxRecParam.SelectedIndex = selectedIndex - 1;
			}
		}

		private void lbxRecordParam_DoubleClick(object sender, EventArgs e)
		{
			this.toLeft();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.cbxFamily.SelectedItem != null)
			{
				try
				{
					this.mForm.CurrentFamily.Database.FamilyTable.Update(this.mForm.CurrentFamily);
					DbFamily dbFamily = (DbFamily)this.cbxFamily.SelectedItem;
					List<DbParamDefinition> list = new List<DbParamDefinition>();
					for (int i = 0; i < this.lbxRecParam.Items.Count; i++)
					{
						DbParamDefinition dbParamDefinition = (DbParamDefinition)this.lbxRecParam.Items[i];
						dbParamDefinition.SetOrder(i + 1);
						list.Add(dbParamDefinition);
					}
					dbFamily.UpdateTypeTableColumn(list);
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message);
				}
			}
			if (this.mForm.CurrentFamily != null)
			{
				this.mForm.ShowType();
			}
			base.Close();
			base.Dispose();
		}

		private void ComparerParamDef(List<DbParamDefinition> paras, ListBox compLbx, ref List<DbParamDefinition> addParams, ref List<DbParamDefinition> removeParams)
		{
			for (int i = 0; i < compLbx.Items.Count; i++)
			{
				DbParamDefinition item = (DbParamDefinition)compLbx.Items[i];
				if (!paras.Contains(item))
				{
					addParams.Add(item);
				}
			}
			for (int j = 0; j < paras.Count; j++)
			{
				DbParamDefinition dbParamDefinition = paras[j];
				if (!compLbx.Items.Contains(dbParamDefinition))
				{
					removeParams.Add(dbParamDefinition);
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void cbxSubject_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void cbxCategory_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void lbxRecordParam_DrawItem(object sender, DrawItemEventArgs e)
		{
			if (e.Index >= 0)
			{
				this.lbxRecParam.DrawMode = DrawMode.OwnerDrawFixed;
				e.DrawBackground();
				Brush brush = Brushes.Black;
				DbParamDefinition dbParamDefinition = (DbParamDefinition)this.lbxRecParam.Items[e.Index];
				if (dbParamDefinition.Limit)
				{
					brush = Brushes.Red;
				}
				if (dbParamDefinition.ParamDefinitionType == ParamDefinitionType.InstanceParam)
				{
					brush = Brushes.Blue;
				}
				else if (dbParamDefinition.ParamDefinitionType == ParamDefinitionType.CustomParam)
				{
					brush = Brushes.Green;
				}
				e.Graphics.DrawString(this.lbxRecParam.Items[e.Index].ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);
				e.DrawFocusRectangle();
			}
		}

		private void cbxFamily_DrawItem(object sender, DrawItemEventArgs e)
		{
		}

		private void cbxFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.lbxParam.Items.Clear();
			this.lbxRecParam.Items.Clear();
			if (this.cbxFamily.SelectedItem != null)
			{
				FamilyLibDatabase database = this.mForm.CurrentSubject.Database;
				DbFamily currentFamily = this.mForm.CurrentFamily;
				List<DbParamDefinition> list = database.FamilyParamTable[currentFamily];
				DbCategory currentCategory = this.mForm.CurrentCategory;
				list.Sort(new ParamDefinitionComp());
				FileInfo fileInfo = database.FamilyFileTable[currentFamily];
				try
				{
					FamilyUtil.GetFamilyTypeParams(this.mForm.UiApp, fileInfo.FullName, this.lbxParam, currentCategory);
				}
				catch
				{
				}
				finally
				{
					fileInfo.Delete();
				}
				for (int i = 0; i < list.Count; i++)
				{
					this.lbxRecParam.Items.Add(list[i]);
				}
				for (int j = 0; j < this.lbxRecParam.Items.Count; j++)
				{
					DbParamDefinition value = (DbParamDefinition)this.lbxRecParam.Items[j];
					if (this.lbxParam.Items.Contains(value))
					{
						this.lbxParam.Items.Remove((DbParamDefinition)this.lbxRecParam.Items[j]);
					}
				}
				this.propertyGrid2.SelectedObject = currentFamily;
			}
		}

		public static DlgEditFamily GetInstance(IDlgBase main)
		{
			if (DlgEditFamily.defp == null || DlgEditFamily.defp.IsDisposed)
			{
				DlgEditFamily.defp = new DlgEditFamily(main);
			}
			return DlgEditFamily.defp;
		}

		private void lbxRecordParam_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lbxRecParam.SelectedItem != null)
			{
				this.propertyGrid1.SelectedObject = (DbParamDefinition)this.lbxRecParam.SelectedItem;
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (this.lbxRecParam.SelectedItem != null)
			{
				int selectedIndex = this.lbxRecParam.SelectedIndex;
				if (selectedIndex > 0)
				{
					DbParamDefinition item = (DbParamDefinition)this.lbxRecParam.SelectedItem;
					this.lbxRecParam.Items.Remove(this.lbxRecParam.SelectedItem);
					this.lbxRecParam.Items.Insert(selectedIndex - 1, item);
					this.lbxRecParam.SelectedIndex = selectedIndex - 1;
				}
			}
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (this.lbxRecParam.SelectedItem != null)
			{
				int selectedIndex = this.lbxRecParam.SelectedIndex;
				if (selectedIndex < this.lbxRecParam.Items.Count - 1)
				{
					DbParamDefinition item = (DbParamDefinition)this.lbxRecParam.SelectedItem;
					this.lbxRecParam.Items.Remove(this.lbxRecParam.SelectedItem);
					this.lbxRecParam.Items.Insert(selectedIndex + 1, item);
					this.lbxRecParam.SelectedIndex = selectedIndex + 1;
				}
			}
		}

		private void DlgEditFamily_Load(object sender, EventArgs e)
		{
			if (this.mForm.CurrentFamily != null)
			{
				this.cbxSubject.Items.Add(this.mForm.CurrentSubject);
				this.cbxSubject.SelectedIndex = 0;
				this.cbxCategory.Items.Add(this.mForm.CurrentFamily.Database.CategoryTable[this.mForm.CurrentFamily.ParentId]);
				this.cbxCategory.SelectedIndex = 0;
				this.cbxFamily.Items.Add(this.mForm.CurrentFamily);
				this.cbxFamily.SelectedIndex = 0;
			}
		}

		private void tbxParamName_TextChanged_1(object sender, EventArgs e)
		{
			if (new Regex("^\\w").Match(this.tbxParamName.Text).Success)
			{
				this.btnAdd.Enabled = true;
				return;
			}
			this.btnAdd.Enabled = false;
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.FileName = this.mForm.CurrentFamily.Name;
			saveFileDialog.Filter = "(*.rfa)|*.rfa";
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				FileInfo fileInfo = this.mForm.CurrentFamily.Database.FamilyFileTable[this.mForm.CurrentFamily];
				try
				{
					fileInfo.CopyTo(saveFileDialog.FileName, true);
					MessageBox.Show("保存完毕");
				}
				catch
				{
					MessageBox.Show("保存失败");
				}
				finally
				{
					fileInfo.Delete();
				}
			}
		}

		private void btnReplace_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = "(*.rfa)|*.rfa";
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
				Document document = this.mForm.UiApp.ActiveUIDocument.Document;
				Transaction transaction = new Transaction(document, "guid");
				try
				{
					Element element = FamilyUtil.LoadFamily(document, fileInfo.FullName);
					transaction.Start();
					transaction.Commit();
					string name = element.Name;
					string name2 = this.mForm.CurrentFamily.Name;
					if (name == name2)
					{
						this.mForm.CurrentFamily.Database.FamilyFileTable.Replace(this.mForm.CurrentFamily, fileInfo);
						Image largeImage = FamilyUtil.GetLargeImage(this.mForm.UiApp.Application, fileInfo.FullName);
						this.mForm.CurrentFamily.Database.FamilyImgTable.UpdateLargeImg(this.mForm.CurrentFamily, largeImage);
						PictureBox pictureBox = new PictureBox();
						FamilyUtil.GetFamilyPreview(this.mForm.UiApp, fileInfo.FullName, pictureBox);
						Image image = pictureBox.Image;
						this.mForm.CurrentFamily.Image = image;
						DbFamily currentFamily = this.mForm.CurrentFamily;
						int verNO = currentFamily.VerNO;
						currentFamily.VerNO = verNO + 1;
						this.mForm.CurrentFamily.Database.FamilyTable.Update(this.mForm.CurrentFamily);
						this.mForm.CurrentFamily.Database.FamilyTable.ReDateByNow(this.mForm.CurrentFamily);
						this.mForm.CurrentFamily.SetDate(DateTime.Now);
						MessageBox.Show("更新完成");
					}
					else
					{
						MessageBox.Show("更新的族跟目标族名称不一致");
					}
				}
				catch
				{
					transaction.RollBack();
					MessageBox.Show("更新失败");
				}
			}
		}

		private void propertyGrid2_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
		{
		}

		private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
		{
		}

		private void button3_Click(object sender, EventArgs e)
		{
			FileInfo fileInfo = this.mForm.CurrentFamily.Database.FamilyFileTable[this.mForm.CurrentFamily];
			FamilyUtil.LoadFamily(this.mForm.UiApp.ActiveUIDocument.Document, fileInfo.FullName);
			Document document = this.mForm.UiApp.ActiveUIDocument.Document;
			FileInfo fi = null;
			this.mForm.CurrentFamily.Load(document, ref fi);
			this.mForm.CurrentFamily.Database.FamilyFileTable.Replace(this.mForm.CurrentFamily, fi);
		}

		private void button4_Click(object sender, EventArgs e)
		{
			FileInfo fileInfo = this.mForm.CurrentFamily.Database.FamilyFileTable[this.mForm.CurrentFamily];
			FamilyUtil.LoadFamily(this.mForm.UiApp.ActiveUIDocument.Document, fileInfo.FullName);
			this.mForm.CurrentFamily.Database.FamilyFileTable.Replace(this.mForm.CurrentFamily, fileInfo);
		}

		private void DlgEditFamily_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private static DlgEditFamily defp;

		private IDlgBase mForm;
	}
}
